define(function(require, exports, module) {
	"use strict";
	require("../../dep/mui/js/mui.min.js");
	require("jquery");
	require("../common.js");
	var template = require("template");
	var startDate = sessionStorage.getItem('startDate'); //开始时间
	var endDate = sessionStorage.getItem('endDate'); //结束时间
	var pageIndex = 1; //当前分页
	var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
	var pageState = 10; //判断有多少页，调用接口再动态改
	mui.init({
		swipeBack: true, //启用右滑关闭功能
		pullRefresh: { //配置分页下拉刷新
			container: '#pullrefresh',
			down: {
				callback: pulldownRefresh
			},
			up: {
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});
	//钉钉返回
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');

	function clearSessionBack() {
		document.location.href = "../../src/Examination/index.html?dd_nav_bgcolor=FFFF943E";
	}

	dingTalkNav.LeftNavBar(clearSessionBack);
	//ad
	dingTalkNav.leftAndroid(clearSessionBack);
	dingTalkNav.RightNavBar(false, "更多", "default");

	//		下滑刷新具体业务实现
	function pulldownRefresh() {
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			loadMore("liststock", "#stocklist", "searchContentTips", false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);
	}
	//		上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			loadMore("liststock", "#stocklist", "searchContentTips", false);
		}, 1500);
	}

	//	$(function(){
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var option = {};
	var sourcetype = sessionStorage.getItem('sourcetype');

	if(abp.sourceType.BUY == sourcetype)
		$(".mui-title").html("采购订单");
	else if(abp.sourceType.SELL == sourcetype)
		$(".mui-title").html("销售订单");
	else if(abp.sourceType.BUY_INPUT == sourcetype)
		$(".mui-title").html("采购入库");
	else if(abp.sourceType.SELL_OUTPUT == sourcetype)
		$(".mui-title").html("销售出库");
	else if(abp.sourceType.WH_ALLOCATE == sourcetype)
		$(".mui-title").html("库存调拨");
	else if(abp.sourceType.WH_CHECK == sourcetype)
		$(".mui-title").html("库存盘点");
	else if(abp.sourceType.WH_INPUT == sourcetype)
		$(".mui-title").html("库存入库");
	else if(abp.sourceType.WH_OUTPUT == sourcetype)
		$(".mui-title").html("库存出库");
	else if(sourcetype === "" || sourcetype === undefined)
		return;
	var loadMore = function(templateId, mainId, contentTips, isChioce) {
		var keyword = $(".keyword").val();
		//var sourcetype=sessionStorage.getItem('sourcetype');
		//sessionStorage.removeItem('sourcetype');
		var dataModel = {
			"startDate": startDate,
			"endDate": endDate,
			"SourceType": sourcetype,
			"orderBy": "",
			"pageIndex": pageIndex,
			"pageSize": "20",
			"type": "1",
			"keyword": ''
		};

		abp.services.app.auditList.getPage(dataModel, option).done(function(data) {
			console.log(data);
			var totalCountNum = data.totalCount; //获取数据列表条数
			pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
			if(totalCountNum < 1) { //动态改变无数据提示
				if(isChioce === true) {
					$('.mui-content > .tipsConInit').html("还没有需要审批的单据哦!");
				}
				$('.mui-content').addClass(contentTips);
				$('.mui-pull-top-pocket').css('display', 'none');
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
			} else {
				$('.mui-content').removeClass(contentTips);
			}
			$.each(data.items, function(index, item) {
				item.dataCreated = item.dataCreated.substring(0, 10);
				item.sourceSerialDate = item.sourceSerialDate.substring(0, 10);
				if(abp.sourceType.BUY == item.sourceType) //采购订单
					item.href = "../purchase/purchaseDetail.html";
				else if(abp.sourceType.SELL == item.sourceType) //销售订单
					item.href = "../salesOrder/salesOrderDetail.html";
				else if(abp.sourceType.BUY_INPUT == item.sourceType) //采购入库
					item.href = "../outPurchase/outPurchase.html";
				else if(abp.sourceType.SELL_OUTPUT == item.sourceType) //销售出库
					item.href = "../outSales/salesDetail.html";
				else if(abp.sourceType.WH_ALLOCATE == item.sourceType) //库存调拨
					item.href = "../allocationStock/allocationStockDetail.html";
				else if(abp.sourceType.WH_CHECK == item.sourceType) //库存盘点
					item.href = "../inventoryRecord/inventoryRecordDetail.html";
				else if(abp.sourceType.WH_INPUT == item.sourceType) //库存入库
					item.href = "../inStock/inStockDetail.html";
				else if(abp.sourceType.WH_OUTPUT == item.sourceType) //库存出库
					item.href = "../outStock/outStockDetail.html";
			});

			var html = template(templateId, data);
			if(dataState === true) { //判断是页面刷新还是上滑追加数据
				$(mainId).html(html);
			} else {
				$(mainId).append(html);
			}

		});
	};
	$(".single-query").click(function() {
		if($(".keyword").val() === "")
			return;
		loadMore("liststock", "#stocklist", "searchContentTips", false);
	});
	loadMore("liststock", "#stocklist", "initContentTips", true);
	mui('body').on('tap', '.mui-table-view-cell a', function() {
		sessionStorage.setItem('SourceId', $(this).attr("SourceId"));
		sessionStorage.setItem('AuditId', $(this).attr("AuditId"));
		sessionStorage.setItem('Audit_detailId', $(this).attr("Audit_detailId"));
		sessionStorage.setItem('SourceType', $(this).attr("SourceType"));
	});

});